package com.opencloud.system.controller;

import com.opencloud.common.constants.CommonConstants;
import com.opencloud.common.model.Result;
import com.opencloud.common.security.XAuthority;
import com.opencloud.common.security.http.XRestTemplate;
import com.opencloud.common.utils.StringUtils;
import com.opencloud.system.client.model.AuthorityApi;
import com.opencloud.system.client.model.AuthorityMenu;
import com.opencloud.system.client.model.AuthorityResource;
import com.opencloud.system.client.model.entity.SystemAuthorityAction;
import com.opencloud.system.client.model.entity.SystemUser;
import com.opencloud.system.client.service.ISystemAuthorityServiceClient;
import com.opencloud.system.service.SystemAuthorityService;
import com.opencloud.system.service.SystemUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author: liuyadu
 * @date: 2018/11/26 18:20
 * @description:
 */
@Api(tags = "系统权限管理")
@RestController
public class SystemAuthorityController implements ISystemAuthorityServiceClient {

    @Autowired
    private SystemAuthorityService systemAuthorityService;
    @Autowired
    private SystemUserService systemUserService;
    @Autowired
    private XRestTemplate xRestTemplate;


    @ApiOperation(value = "获取所有访问权限列表", notes = "获取所有访问权限列表")
    @GetMapping("/authority/access")
    @Override
    public Result<List<AuthorityResource>> findAuthorityResource() {
        List<AuthorityResource> result = systemAuthorityService.findAuthorityResource();
        return Result.ok().data(result);
    }

    @ApiOperation(value = "获取接口权限列表", notes = "获取接口权限列表")
    @GetMapping("/authority/api")
    @Override
    public Result<List<AuthorityApi>> findAuthorityApi(
            @RequestParam(value = "serviceId", required = false) String serviceId
    ) {
        List<AuthorityApi> result = systemAuthorityService.findAuthorityApi(serviceId);
        return Result.ok().data(result);
    }

    @ApiOperation(value = "获取菜单权限列表", notes = "获取菜单权限列表")
    @GetMapping("/authority/menu")
    @Override
    public Result<List<AuthorityMenu>> findAuthorityMenu() {
        List<AuthorityMenu> result = systemAuthorityService.findAuthorityMenu(1);
        return Result.ok().data(result);
    }


    @ApiOperation(value = "获取功能权限列表", notes = "获取功能权限列表")
    @GetMapping("/authority/action")
    @Override
    public Result<List<SystemAuthorityAction>> findAuthorityAction(
            @RequestParam(value = "actionId") Long actionId
    ) {
        List<SystemAuthorityAction> list = systemAuthorityService.findAuthorityAction(actionId);
        return Result.ok().data(list);
    }


    @ApiOperation(value = "获取角色已分配权限", notes = "获取角色已分配权限")
    @GetMapping("/authority/role")
    @Override
    public Result<List<XAuthority>> findAuthorityRole(@RequestParam(value = "roleId") Long roleId) {
        List<XAuthority> result = systemAuthorityService.findAuthorityByRole(roleId);
        return Result.ok().data(result);
    }

    @ApiOperation(value = "获取用户菜单列表", notes = "获取用户菜单列表")
    @GetMapping("/authority/user/menu")
    @Override
    public Result<List<AuthorityMenu>> findAuthorityMenuByUser(@RequestParam(value = "userId") Long userId,@RequestParam(value = "root")  Boolean root) {
        return Result.ok().data(systemAuthorityService.findAuthorityMenuByUser(userId,root));
    }

    @ApiOperation(value = "获取用户已分配权限", notes = "获取用户已分配权限")
    @GetMapping("/authority/user")
    @Override
    public Result<List<XAuthority>> findAuthorityUser(
            @RequestParam(value = "userId") Long userId
    ) {
        SystemUser user = systemUserService.getById(userId);
        List<XAuthority> result = systemAuthorityService.findAuthorityByUser(userId, CommonConstants.ROOT.equals(user.getUserName()));
        return Result.ok().data(result);
    }

    @ApiOperation(value = "获取应用已分配接口权限", notes = "获取应用已分配接口权限")
    @GetMapping("/authority/app")
    @Override
    public Result<List<XAuthority>> findAuthorityApp(
            @RequestParam(value = "appId") String appId
    ) {
        List<XAuthority> result = systemAuthorityService.findAuthorityByApp(appId);
        return Result.ok().data(result);
    }

    @ApiOperation(value = "分配角色权限", notes = "分配角色权限")
    @PostMapping("/authority/role/grant")
    @Override
    public Result grantAuthorityRole(
            @RequestParam(value = "roleId") Long roleId,
            @RequestParam(value = "expireTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date expireTime,
            @RequestParam(value = "authorityIds", required = false) String authorityIds
    ) {
        systemAuthorityService.addAuthorityRole(roleId, expireTime, StringUtils.isNotBlank(authorityIds) ? authorityIds.split(",") : new String[]{});
        xRestTemplate.refreshGateway();
        return Result.ok();
    }

    @ApiOperation(value = "分配用户权限", notes = "分配用户权限")
    @PostMapping("/authority/user/grant")
    @Override
    public Result grantAuthorityUser(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "expireTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date expireTime,
            @RequestParam(value = "authorityIds", required = false) String authorityIds
    ) {
        systemAuthorityService.addAuthorityUser(userId, expireTime, StringUtils.isNotBlank(authorityIds) ? authorityIds.split(",") : new String[]{});
        xRestTemplate.refreshGateway();
        return Result.ok();
    }

    @ApiOperation(value = "分配应用权限", notes = "分配应用权限")
    @PostMapping("/authority/app/grant")
    @Override
    public Result grantAuthorityApp(
            @RequestParam(value = "appId") String appId,
            @RequestParam(value = "expireTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date expireTime,
            @RequestParam(value = "authorityIds", required = false) String authorityIds
    ) {
        systemAuthorityService.addAuthorityApp(appId, expireTime, StringUtils.isNotBlank(authorityIds) ? authorityIds.split(",") : new String[]{});
        xRestTemplate.refreshGateway();
        return Result.ok();
    }

    @ApiOperation(value = "功能按钮授权", notes = "功能按钮授权")
    @PostMapping("/authority/action/grant")
    @Override
    public Result grantAuthorityAction(
            @RequestParam(value = "actionId") Long actionId,
            @RequestParam(value = "authorityIds", required = false) String authorityIds
    ) {
        systemAuthorityService.addAuthorityAction(actionId, StringUtils.isNotBlank(authorityIds) ? authorityIds.split(",") : new String[]{});
        xRestTemplate.refreshGateway();
        return Result.ok();
    }


}
